package com.shenmazong.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shenmazong.dto.TbSoftware;
import org.redisson.Redisson;
import org.redisson.api.LocalCachedMapOptions;
import org.redisson.api.RLocalCachedMap;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * @author 阿瑞
 * @version 1.0
 * @description TODO
 * @class SoftwareReceive ???
 * @date 2021/9/26 10:51
 */
@Component
public class SoftwareReceive {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    private Redisson redisson;
    @RabbitListener(queues = MqConfig.SOFTWARE_QUEUE)
    public void  receiveJob(String msg){
        ObjectMapper objectMapper = new ObjectMapper();
        HashMap<String,String> map=null;
        try {
            map = objectMapper.readValue(msg, HashMap.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        RLocalCachedMap<String, String> botian = redisson.getLocalCachedMap("software", LocalCachedMapOptions.defaults());
        List list = null;
        try {
            list = objectMapper.readValue(botian.get("software"), List.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        if (map.get("saveSoftware")!=null){
            String saveSoftware = map.get("saveSoftware");
            try {
                TbSoftware tbSoftware = objectMapper.readValue(saveSoftware, TbSoftware.class);
                 elasticsearchRestTemplate.save(tbSoftware);
                list.add(tbSoftware);
                String s = objectMapper.writeValueAsString(list);
                botian.put("software",s);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }else if (map.get("auditSoftware")!=null){
            String s = map.get("auditSoftware");
            TbSoftware software =null;
            try {
                software = objectMapper.readValue(s, TbSoftware.class);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            for (Object o : list) {
                try {
                    String s1 = objectMapper.writeValueAsString(o);
                    TbSoftware tbSoftware = objectMapper.readValue(s1, TbSoftware.class);
                    if (tbSoftware.getSoftwareId()==software.getSoftwareId()){
                        list.remove(o);
                    }
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }
            elasticsearchRestTemplate.save(software);
        }else{
            String s = map.get("delSoftware");
            for (Object o : list) {
                try {
                    String s1 = objectMapper.writeValueAsString(o);
                    TbSoftware tbSoftware = objectMapper.readValue(s1, TbSoftware.class);
                    if (tbSoftware.getSoftwareId()==Long.parseLong(s)){
                        System.out.println("tbSoftware = " + tbSoftware);
                        tbSoftware.setDeleteFlag((byte) 1);
                        elasticsearchRestTemplate.save(tbSoftware);
                        list.remove(o);
                    }
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }
            try {
                botian.put("software",objectMapper.writeValueAsString(list));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }
}
